SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE Procedure [dbo].[SP_TCDOCU_PGGR_I01]   
/*-------------------------------------------------------*/        
/*---Empresa              : OFISIS S.A.                --*/        
/*---Cliente              : OFISIS                 --*/        
/*---Sistema              : Tesoreria                  --*/        
/*---M¸dulo               : Empresa                    --*/        
/*---Programa             : Grabaci©n Cuotas a la tabla--*/         
/*---                       Cuentas x pagar            --*/         
/*---Script               : tcmovi_b.sql               --*/        
/*---Nombre SP            : SP_TCDOCU_PGGR_I01         --*/        
/*---Desarrollado por     : Javier Flores A.           --*/        
/*---Fecha Creaci¸n       : 16/11/1999                 --*/        
/*---Base Datos           : Microsoft Sql Server       --*/        
/*---Versi¸n              : 6.5                        --*/        
/*---Invoca a SP          :                            --*/        
/*-------------------------------------------------------*/        
/*-------------------------------------------------------*/        
/*---Modificado 1 por     : Denis Villacr's P.         --*/        
/*---Fecha Modificaci¸n   : 29/12/2000                 --*/         
/*---Detalle Modificaci¸n : se aumento a 03 digitos al --*/        
/*---       gener. CMP en TMDOCU_PROV  --*/        
/*-------------------------------------------------------*/        
/*-------------------------------------------------------*/        
/*---Modificado 2 por     : Maribel Julca Rodriguez    --*/        
/*---Fecha Modificaci©n   : 21/04/2005                 --*/         
/*---Detalle Modificaci©n : ingreso de los campos CO_USUA_CREA, CO_USUA_MODI  --*/        
/*-------------------------------------------------------*/        
/*-------------------------------------------------------*/        
/*---Modificado 3 por     :  Carlos Marquez Escobar  --*/        
/*---Fecha Modificaci©n   : 03/02/2006                  --*/         
/*---Detalle Modificaci©n : Corrige la fecha de RegComp --*/        
/*-------------------------------------------------------*/        
/*-------------------------------------------------------*/        
/*---Comentarios                                       --*/        
/*---                                                  --*/        
/*---                                                  --*/        
/*-------------------------------------------------------*/        
/*-------------------------------------------------------*/        
/*--- Drop Proc SP_TCDOCU_PGGR_I01                     --*/        
/*--- Grant all on SP_TCDOCU_PGGR_I01 to public        --*/         
/*--- SP_TCDOCU_PGGR_I01                               --*/        
/*-------------------------------------------------------*/        
 @ISCO_EMPR         TD_VC_002,         
 @ISCO_USUA_MODI    TD_VC_008_USUA,        
 @ISTI_DOCU        TD_VC_003,        
 @ISNU_DOCU        TD_VC_020,        
 @ISNU_CORR_DOCU    TD_VC_008,    
 @ISCO_TIPO_OPER    TD_VC_003,        
 @ISCO_PROV         TD_VC_020,        
 @ISCO_MONE         TD_VC_003,        
 @INFA_TIPO_CAMB    TD_NU_015_006,        
 @ISCO_UNID_CNTB    TD_VC_003,        
 @ISDE_OBSE         TD_VC_100,        
 @IDFE_EMIS_COMP        TD_DT_001    
 As    
Declare         
 @CSNU_DOCU  TD_VC_020,         
 @CNNU_CUOT_DETA  TD_IN_001,         
 @CDFE_VENC_CUOT  TD_DT_001,         
 @CNIM_CUOT       TD_NU_016_004,        
 @VNIM_DIST       TD_NU_016_004,        
 @VNNU_FETC_0002  TD_IN_001,        
 @VNNU_FETC_0001  TD_IN_001,        
 @CNNU_CORR       TD_IN_001,        
 @CNPO_DIST       TD_NU_009_004,        
 @CSTI_AUXI_EMPR  TD_VC_001,        
 @CNIM_CUOT_DIST  TD_NU_016_004,        
 @CSCO_AUXI_EMPR  TD_VC_020,        
 @CSCO_CNTA_EMPR  TD_VC_010,        
 @CSCO_ORDE_SERV  TD_VC_020,        
 @CSST_DIST       TD_VC_001,        
 @CSTI_DOCU  TD_VC_003,    
 @VSTI_OPER_CARG  TD_VC_003,    
 @VSCO_COND_CONT  TD_VC_003      
        
    
 SELECT @VSTI_OPER_CARG = CO_TIPO_OPEG, @VSCO_COND_CONT = ISNULL(CO_COND_CONT, '')    
 FROM TMPARA_TESO    
 WHERE CO_EMPR = @ISCO_EMPR    
    
 BEGIN TRAN        
        
 Declare CU_TCDOCU_PGGR_I01_1 cursor         
 For         
 Select  TI_DOCU, NU_DOCU, NU_CUOT_DETA, FE_VENC_CUOT, IM_CUOT        
 From    TDDOCU_PGGR       
 Where   CO_EMPR = @ISCO_EMPR        
 And     TI_DOCU = @ISTI_DOCU        
 And     NU_DOCU = @ISNU_DOCU        
     
 Open CU_TCDOCU_PGGR_I01_1        
 Fetch CU_TCDOCU_PGGR_I01_1 Into         
 @CSTI_DOCU, @CSNU_DOCU, @CNNU_CUOT_DETA, @CDFE_VENC_CUOT, @CNIM_CUOT        
     
 Select @VNNU_FETC_0001 = @@FETCH_STATUS        
     
 While (@VNNU_FETC_0001 = 0)        
 Begin        
  If (@VNNU_FETC_0001 < 0)        
  Begin        
   /*RAISERROR 20001 --"FIN DE ARCHIVO"*/        
   Close CU_TCDOCU_PGGR_I01_1        
   Deallocate CU_TCDOCU_PGGR_I01_1        
   Return        
  End        
      
  Insert Into TMDOCU_PROV (CO_EMPR,CO_PROV, CO_TIPO_DOCU, NU_DOCU_PROV, CO_TIPO_OPER,        
  CO_COND_PAGO, CO_UNID_CONC, FE_VENC, FE_EMIS, FE_PROG_PAGO, FA_TIPO_CAMB, CO_MONE, IM_BRUT_AFEC,        
  IM_TOTA, CO_ESTA_DOCU, ST_CONT, FE_REGI_COMP, PC_DCT1, PC_DCT2, CO_USUA_CREA,FE_USUA_CREA, CO_USUA_MODI, FE_USUA_MODI)        
  Values (@ISCO_EMPR, @ISCO_PROV, @ISTI_DOCU, @CSNU_DOCU+'-'+replicate ('0', 3-len(rtrim(convert(varchar, @CNNU_CUOT_DETA))))+convert(varchar, @CNNU_CUOT_DETA),        
  @VSTI_OPER_CARG, @VSCO_COND_CONT, @ISCO_UNID_CNTB, @CDFE_VENC_CUOT, @IDFE_EMIS_COMP,         
  @CDFE_VENC_CUOT, @INFA_TIPO_CAMB, @ISCO_MONE, @CNIM_CUOT, @CNIM_CUOT, 'ACT', 'N', @IDFE_EMIS_COMP,         
  0, 0, @ISCO_USUA_MODI,GETDATE(), @ISCO_USUA_MODI, GETDATE())        
      
  IF (@@ERROR != 0)        
  BEGIN        
   /* RAISERROR 20001-- "ERROR DE INSERCION"*/        
   Close CU_TCDOCU_PGGR_I01_1        
   Deallocate CU_TCDOCU_PGGR_I01_1         
   ROLLBACK TRAN        
   RETURN        
  END        
      
  Declare CU_TCDOCU_PGGR_I01_2 cursor         
  For         
  Select  NU_CORR, TI_AUXI_EMPR, CO_AUXI_EMPR, CO_CNTA_EMPR, CO_ORDE_SERV, ST_DIST,         
  PO_DIST, IM_CUOT_DIST        
  From    TDDIST_PGGR        
  Where   CO_EMPR = @ISCO_EMPR        
  And     TI_DOCU  = @CSTI_DOCU        
  And     NU_DOCU = @CSNU_DOCU        
      
  Open CU_TCDOCU_PGGR_I01_2        
  Fetch CU_TCDOCU_PGGR_I01_2 Into         
  @CNNU_CORR, @CSTI_AUXI_EMPR, @CSCO_AUXI_EMPR, @CSCO_CNTA_EMPR,        
  @CSCO_ORDE_SERV, @CSST_DIST, @CNPO_DIST, @CNIM_CUOT_DIST        
      
  SELECT @VNNU_FETC_0002 = @@FETCH_STATUS        
      
  While (@VNNU_FETC_0002 = 0)        
  Begin        
   If (@VNNU_FETC_0002 < 0)        
   Begin        
    /*  RAISERROR 20001 --"FIN DE ARCHIVO"  */        
    Close CU_TCDOCU_PGGR_I01_1        
    Deallocate CU_TCDOCU_PGGR_I01_1         
    Close CU_TCDOCU_PGGR_I01_2        
    Deallocate CU_TCDOCU_PGGR_I01_2        
    Return        
   End        
      
   Select @VNIM_DIST = 0        
   Select @VNIM_DIST =  Round(( Round( @CNPO_DIST * @CNIM_CUOT,2))/100,2)        
       
   Insert Into TDDISP_CCOS (CO_EMPR, CO_PROV, CO_TIPO_DOCU, NU_DOCU_PROV,         
   NU_CORR, TI_AUXI_EMPR, CO_CENT_COST, CO_CNTA_EMPR, CO_ORDE_SERV,         
   ST_DIST, PO_DIST, IM_DIST, DE_OBSE, CO_USUA_CREA,FE_USUA_CREA, CO_USUA_MODI, FE_USUA_MODI)        
   Values (@ISCO_EMPR, @ISCO_PROV, @CSTI_DOCU, @CSNU_DOCU+'-'+replicate ('0', 3-len(rtrim(convert(varchar, @CNNU_CUOT_DETA))))+convert(varchar, @CNNU_CUOT_DETA),        
   @CNNU_CORR, @CSTI_AUXI_EMPR, @CSCO_AUXI_EMPR, @CSCO_CNTA_EMPR,         
   @CSCO_ORDE_SERV, @CSST_DIST, @CNPO_DIST, @VNIM_DIST, @ISDE_OBSE, @ISCO_USUA_MODI,GETDATE(), @ISCO_USUA_MODI, GETDATE())        
      
   IF (@@ERROR != 0)        
   BEGIN        
    /*   RAISERROR 20001 --"ERROR DE INSERCION" */        
    Close CU_TCDOCU_PGGR_I01_1        
    Deallocate CU_TCDOCU_PGGR_I01_1         
    Close CU_TCDOCU_PGGR_I01_2        
    Deallocate CU_TCDOCU_PGGR_I01_2        
    ROLLBACK TRAN        
   RETURN        
      
  END        
      
  Fetch  CU_TCDOCU_PGGR_I01_2         
  Into  @CNNU_CORR, @CSTI_AUXI_EMPR, @CSCO_AUXI_EMPR, @CSCO_CNTA_EMPR,         
  @CSCO_ORDE_SERV, @CSST_DIST, @CNPO_DIST, @CNIM_CUOT_DIST        
      
  SELECT @VNNU_FETC_0002 = @@FETCH_STATUS        
     
 End        
     
 Close CU_TCDOCU_PGGR_I01_2        
 Deallocate CU_TCDOCU_PGGR_I01_2        
     
 Fetch  CU_TCDOCU_PGGR_I01_1         
 Into  @CSTI_DOCU, @CSNU_DOCU, @CNNU_CUOT_DETA, @CDFE_VENC_CUOT, @CNIM_CUOT        
     
 SELECT @VNNU_FETC_0001 = @@FETCH_STATUS        
     
 End        
     
 Close CU_TCDOCU_PGGR_I01_1        
 Deallocate CU_TCDOCU_PGGR_I01_1         
        
 Commit        
        
Return         
/*--------------------------- Fin -------------------------------- */    
    
GO
